import app from '@system.app';
import router from '@system.router';
import http from '@ohos.net.http';
import dataStorage from '@ohos.data.storage'
import featureAbility from '@ohos.ability.featureAbility'

export default {
    data: {
        image1src: "/common/login/login1.png",
        image2src: "/common/login/login2.png",
        image3src: "/common/login/login3.png",
        title: 'World',
        username: 1,
        password: 1,
        rusername: 0,
        rpassword: 0,
        sit: 0,
    },
    userNameOnChange(getUserName) {
        this.rusername = getUserName.value;
    },
    passWordOnChange(getPassWord) {
        this.rpassword = getPassWord.value;
    },
    buttonClick() {
        if (this.sit == 0) {
            this.$element('dragDialog1').show();
        }
        else {
            let store = dataStorage.getStorageSync('/data/data/com.example.imitationtaobao/files/token');
            store.get('token', 0).then((value) => {
                //有token就直接发token
                let httpRequest = http.createHttp();
                httpRequest.request(this.$app.$def.globalData.baseURL + "/login", {
                    method: 'POST',
                    header: {
                        'Content-Type': 'application/json'
                    },
                    extraData: {
                        "token": value
                    }
                }, (err, data) => {
                    if (!err) {
                        console.info('Result:' + data.result);
                        if (data.result.code == 0) {
                            router.push({
                                uri: "pages/shouye/shouye"
                            })
                        } else {
                            console.log("账户名或密码错误" + this.rpassword + this.rusername);
                            this.$element('dragDialog').show();
                        }
                    } else {
                        console.log('error:' + err.data);
                    }
                });
            }).catch((err) => {
                //没token就正常登录
                console.info("抓取失败 " + err);
                let httpRequest = http.createHttp();
                httpRequest.request(this.$app.$def.globalData.baseURL + "/login", {
                    method: 'POST',
                    header: {
                        'Content-Type': 'application/json'
                    },
                    extraData: {
                        "username": this.rusername,
                        "password": this.rpassword
                    }
                }, (err, data) => {
                    if (!err) {
                        console.info('Result:' + data.result);
                        if (data.result.code == 0) {
                            //成功就存token
                            var context = featureAbility.getContext();
                            context.getFilesDir((err, path) => {
                                if (err) {
                                    console.error('getFilesDir failed. err: ' + JSON.stringify(err));
                                    return;
                                }
                                console.info('path:' + path) // /data/data/com.iss.myapplication/files
                                let store = dataStorage.getStorageSync(path + '/mystore');
                                store.putSync('token', data.result.data.token);
                                store.flushSync();
                                router.push({
                                    uri: "pages/qidong/qidong",
                                });
                            });
                            router.push({
                                uri: "pages/shouye/shouye"
                            })
                        } else {
                            console.log("账户名或密码错误" + this.rpassword + this.rusername);
                            this.$element('dragDialog').show();
                        }
                    } else {
                        console.log('error:' + err.data);
                    }
                });

            });
        }
    },
    register() {
        router.push({
            uri: 'pages/register/register',
        })
    },
    setSchedule() {
        this.$element('dragDialog').close();
    },
    setSchedule1() {
        this.$element('dragDialog1').close();
    },
    imageonclick() {
        if (this.sit == 0) {
            this.sit = 1;
            this.image3src = "/common/login/login4.png";
        }
        else {
            this.sit = 0;
            this.image3src = "/common/login/login3.png";
        }
    },
    tuichu() {
        app.terminate();
    }
}
